package com.wdd.libhttp.http


import com.blankj.utilcode.util.GsonUtils
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.HttpParams
import com.lzy.okrx2.adapter.ObservableBody
import com.wdd.libhttp.rx.RxSchedulers

import io.reactivex.Observable

/**
 *
 * @ProjectName:    OwnTool
 * @Package:        com.wdd.libbase.http
 * @ClassName:      HttpAskHelper
 * @Description:     网络请求封装
 * @Author:         王丹丹
 * @CreateDate:     2019/12/5 11:13
 * @UpdateUser:     更新者
 * @UpdateDate:     2019/12/5 11:13
 * @UpdateRemark:   更新说明
 * @Version:        1.0
 */
class HttpAskHelper {
    companion object {
        private var instance: HttpAskHelper? = null
            get() {
                if (field == null) {
                    field = HttpAskHelper()
                }
                return field
            }

        @Synchronized
        fun instance(): HttpAskHelper {
            return instance!!
        }
    }


    var encryptionImpAll: EncryptionInterface? = null


    /**
     * get请求
     */
    fun getHttp(
        url: String,
        params: Map<String, Any>? = null,
        encryptionImp: EncryptionInterface? = null
    ): Observable<String> {
        val httpParams = createHttpParams(params)
        return OkGo.get<String>(url).params(httpParams)
            .converter(StringConvert(encryptionImp ?: encryptionImpAll))
            .adapt(ObservableBody<String>()).compose(RxSchedulers.applySchedulers<String>())
    }


    /**
     * post oarams请求
     */
    fun postParams(
        url: String,
        params: Map<String, Any>? = null,
        encryptionImp: EncryptionInterface? = null
    ): Observable<String> {
        val httpParams = createHttpParams(params)
        return OkGo.post<String>(url).params(httpParams)
            .converter(StringConvert(encryptionImp ?: encryptionImpAll))
            .adapt(ObservableBody<String>()).compose(RxSchedulers.applySchedulers<String>())
    }

    /**
     * post body请求
     */
    fun postBody(
        url: String,
        params: Map<String, Any>? = null,
        encryptionImp: EncryptionInterface? = null
    ): Observable<String> {
        val postRequest = OkGo.post<String>(url)
        if (params != null) {
            val bodyStr = GsonUtils.getGson().toJson(params)
            postRequest.upJson(bodyStr)
        }
        return postRequest.converter(StringConvert(encryptionImp ?: encryptionImpAll))
            .adapt(ObservableBody<String>()).compose(RxSchedulers.applySchedulers<String>())
    }

    /**
     * 创建请求参数
     */
    private fun createHttpParams(params: Map<String, Any>?): HttpParams {
        val httpParams = HttpParams()
        params?.forEach {
            when (val data = it.value) {
                is Int -> {
                    httpParams.put(it.key, data)
                }
                is Long -> {
                    httpParams.put(it.key, data)
                }
                is String -> {
                    httpParams.put(it.key, data)
                }
                is Double -> {
                    httpParams.put(it.key, data)
                }
                is Float -> {
                    httpParams.put(it.key, data)
                }
                else -> {
                    httpParams.put(it.key, GsonUtils.getGson().toJson(data))
                }
            }

        }
        return httpParams
    }

}